using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MagnoliaTableAdapters;

public partial class ScheduleList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = ConfigurationManager.AppSettings["PageTitle"] + " - Schedule List";

        if (!Page.IsPostBack)
        {
            Btn_AddDay.Attributes.Add("style", "width:100px");
            Btn_AddWeek.Attributes.Add("style", "width:100px");
            Calendar_Schedules.Attributes.Add("style", "margin-top:1px");
            Calendar_Schedules.SelectedDate = DateTime.Now;
            //Calendar_Schedules.Attributes.Add("ondblclick", "addDay();");
            if (ListBox_Selected.Items.Count > 0)
            {
                LoadInfoPanel();

                //Decimal scheduledHours = GetScheduledHours();
                //NameLabel.Text = "<strong>Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
                ////NameLabel.Text += "<strong>Last Modified: </strong>08/15/07 by leighs<br />";
                //NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
                //NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";
            }
        }
    }
    protected void Btn_AddDay_Click(object sender, EventArgs e)
    {
        DateTime selectedDate = Calendar_Schedules.SelectedDate;
        string strDate = selectedDate.ToString("M/d/yy");
        if (ListBox_Selected.Items.Count < 7)
        {
            bool inList = false;
            int index = 0;
            foreach (ListItem item in ListBox_Selected.Items)
            {
                if (Convert.ToDateTime(strDate) > Convert.ToDateTime(item.Value))
                {
                    index++;
                }
                else if (strDate == item.Value)
                {
                    inList = true;
                    break;
                }
            }
            if (!inList)
            {
                
                //ListBox_Selected.Items.Add(strDate);
                ListBox_Selected.Items.Insert(index, strDate);
                ListBox_Selected.SelectedIndex = index;

                LoadInfoPanel();

                //Decimal scheduledHours = GetScheduledHours();
                //NameLabel.Text = "<strong>Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
                ////NameLabel.Text += "<strong>Last Modified: </strong>08/15/07 by leighs<br />";
                //NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
                //NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";
            }
        }
        Enable_Disable_Buttons();

    }
    protected void Btn_AddWeek_Click(object sender, EventArgs e)
    {
        foreach (DateTime dt in Calendar_Schedules.SelectedDates)
        {
            string strDate = dt.ToString("M/d/yy");
            if (ListBox_Selected.Items.Count < 7)
            {
                bool inList = false;
                foreach (ListItem item in ListBox_Selected.Items)
                {
                    if (strDate == item.Value)
                    {
                        inList = true;
                        break;
                    }
                }
                if (!inList)
                {
                    ListBox_Selected.Items.Add(strDate);
                }

            }
        }
        if (ListBox_Selected.Items.Count > 0)
        {
            ListBox_Selected.SelectedIndex = 0;

            LoadInfoPanel();

            //Decimal scheduledHours = GetScheduledHours();
            //NameLabel.Text = "<strong>Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
            ////NameLabel.Text += "<strong>Last Modified: </strong>08/15/07 by leighs<br />";
            //NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
            //NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";
        }
        Enable_Disable_Buttons();
    }
    protected void Btn_Remove_Click(object sender, EventArgs e)
    {
        ListBox_Selected.Items.Remove(ListBox_Selected.SelectedItem);
        if (ListBox_Selected.Items.Count > 0)
        {
            ListBox_Selected.SelectedIndex = 0;
            LoadInfoPanel();
            //Decimal scheduledHours = GetScheduledHours();
            //NameLabel.Text = "<strong>Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
            ////NameLabel.Text += "<strong>Last Modified: </strong>08/15/07 by leighs<br />";
            //NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
            //NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";
        }
        else
        {
            NameLabel.Text = "";
        }
        Enable_Disable_Buttons();
    }

    protected void Enable_Disable_Buttons()
    {
        if (ListBox_Selected.Items.Count >= 7)
        {
            Btn_AddDay.Enabled = false;
            Btn_AddWeek.Enabled = false;
        }
        else if (ListBox_Selected.Items.Count > 0)
        {
            Btn_AddDay.Enabled = true;
            Btn_AddWeek.Enabled = false;
        }
        else
        {
            Btn_AddDay.Enabled = true;
            Btn_AddWeek.Enabled = true;
            Btn_Edit.Enabled = false;
            Btn_EditAll.Enabled = false;
            Btn_Remove.Enabled = false;
            Btn_RemoveAll.Enabled = false;
        }

        if (ListBox_Selected.SelectedIndex == -1)
        {
            Btn_Edit.Enabled = false;
            Btn_EditAll.Enabled = false;
            Btn_Remove.Enabled = false;
            Btn_RemoveAll.Enabled = false;
        }
        else
        {
            Btn_Edit.Enabled = true;
            Btn_EditAll.Enabled = true;
            Btn_Remove.Enabled = true;
            Btn_RemoveAll.Enabled = true;
        }
    }
    protected void Btn_EditAll_Click(object sender, EventArgs e)
    {
        Boolean isWeek = false;
        if (ListBox_Selected.Items.Count == 7)
        {
            for (int i=0; i<ListBox_Selected.Items.Count; i++)
            {
                DateTime date = Convert.ToDateTime(ListBox_Selected.Items[i].Value);
                if (i == 0)
                {
                    if (date.DayOfWeek != DayOfWeek.Monday)  // assume week is based on monday
                    {
                        break;
                    }
                }
                else if (i>0)
                {
                    DateTime prevDate = Convert.ToDateTime(ListBox_Selected.Items[i-1].Value);
                    if (prevDate.AddDays(1) != date)
                    {
                        break;
                    }
                }
                isWeek = true;
            }
        }
        if (isWeek)
        {
            Hidden_BtnChoice.Value = "EditWeek";
        }
        else
        {
            Hidden_BtnChoice.Value = "EditAll";
        }
    }
    protected void ListBox_Selected_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadInfoPanel();

        //Decimal scheduledHours = GetScheduledHours();
        //NameLabel.Text = "<strong>Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
        //NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
        //NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";

        //SCHEDULEACTIVITYLOGTableAdapter ActivityAdapter = new SCHEDULEACTIVITYLOGTableAdapter();
        //Magnolia.SCHEDULEACTIVITYLOGDataTable dtActivity = new Magnolia.SCHEDULEACTIVITYLOGDataTable();
        //String selectedDate = (Convert.ToDateTime(ListBox_Selected.SelectedValue)).ToString("yyyy-MM-dd");
        //ActivityAdapter.Fill(dtActivity, selectedDate);

        //NameLabel.Text += "<strong>Last Modified: </strong>" + Convert.ToString(dtActivity.Rows[0].ItemArray[0]) + "<br />";
        //NameLabel.Text += "<strong>Last Modified By: </strong>" + Convert.ToString(dtActivity.Rows[0].ItemArray[1]) + "<br />";
        
        //Enable_Disable_Buttons();
    }

    protected void LoadInfoPanel()
    {
        Decimal scheduledHours = GetScheduledHours();
        NameLabel.Text = "<strong>Schedule Date: </strong>" + ListBox_Selected.SelectedValue + "<br />";
        NameLabel.Text += "<strong>Scheduled Hours: </strong>" + Convert.ToString(scheduledHours) + "<br />";
        NameLabel.Text += "<strong>Unscheduled Hours: </strong>" + Convert.ToString(24 - scheduledHours) + "<br />";

        SCHEDULEACTIVITYLOGTableAdapter ActivityAdapter = new SCHEDULEACTIVITYLOGTableAdapter();
        Magnolia.SCHEDULEACTIVITYLOGDataTable dtActivity = new Magnolia.SCHEDULEACTIVITYLOGDataTable();
        String selectedDate = (Convert.ToDateTime(ListBox_Selected.SelectedValue)).ToString("yyyy-MM-dd");
        ActivityAdapter.Fill(dtActivity, selectedDate);
        if (dtActivity.Rows.Count > 0)
        {
            NameLabel.Text += "<strong>Last Modified: </strong>" + Convert.ToString(dtActivity.Rows[0].ItemArray[1]) + "<br />";
            NameLabel.Text += "<strong>Last Modified By: </strong>" + Convert.ToString(dtActivity.Rows[0].ItemArray[0]) + "<br />";
        }
        else
        {
            NameLabel.Text += "<strong>Last Modified: </strong><br />";
            NameLabel.Text += "<strong>Last Modified By: </strong><br />";
        }
        Enable_Disable_Buttons();
    }

    protected Decimal GetScheduledHours()
    {
        SCHEDULEDSPOTTableAdapter SpotAdapter = new SCHEDULEDSPOTTableAdapter();
        DateTime dateStart = Convert.ToDateTime(ListBox_Selected.SelectedValue);
        DateTime dateEnd = Convert.ToDateTime(dateStart).AddDays(1);
        Decimal numSpots = Convert.ToDecimal(SpotAdapter.ScalarQuery(dateStart, dateEnd));
        Decimal scheduledHours = numSpots / 2;
        return scheduledHours;
    }

    protected void Btn_RemoveAll_Click(object sender, EventArgs e)
    {
        ListBox_Selected.Items.Clear();

        ListBox_Selected.SelectedIndex = -1;
        NameLabel.Text = "";
        Enable_Disable_Buttons();
    }
}
